﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.Xml;

namespace Fawcett.Plugins
{
    
    public class Mapping : IMapping
    {
        public string Name { get; set; }
        public string Offset { get; set; }
        public string Threshold { get; set; }
        public Color On { get; set; }
        public Color Off { get; set; }
        public Color BackOn { get; set; }
        public Color BackOff { get; set; }
        static public MappingCollection Map(System.Xml.XmlNode section)
        {
            MappingCollection maps = new MappingCollection();

            foreach (XmlNode node in section.ChildNodes)
            {
                try
                {
                    maps.Add(new Mapping()
                    {
                        Name = node.Attributes["Name"].Value,
                        Offset = node.Attributes["Offset"].Value,
                        Threshold = node.Attributes["Threshold"].Value,
                        On = Color.FromName( node.Attributes["On"].Value),
                        Off =Color.FromName( node.Attributes["Off"].Value),
                        BackOn = Color.FromName(node.Attributes["BackOn"].Value),
                        BackOff = Color.FromName(node.Attributes["BackOff"].Value)
                    });
                }
               catch (Exception e)
               {
                   string str = String.Format(Fawcett.Plugins.Properties.Resources.ERROR_3, section.Name);
                   Fawcett.Plugins.MessageBox.Show(e, str);
               }
            }
            return maps;
        }
    }

    public interface IMapping
    {
        string Name { get; set; }
        string Offset { get; set; }
        string Threshold { get; set; }
         Color On { get; set; }
        Color Off { get; set; }
        Color BackOn { get; set; }
        Color BackOff { get; set; }
    }

    public class MappingCollection : CollectionBase
    {
        public MappingCollection()
        {
        }

        public MappingCollection(MappingCollection value)
        {
            this.AddRange(value);
        }

        public MappingCollection(IMapping[] value)
        {
            this.AddRange(value);
        }


        public IMapping this[int index]
        {
            get { return ((IMapping)(this.List[index])); }
        }

        public int Add(IMapping value)
        {
            return this.List.Add(value);
        }


        public void AddRange(IMapping[] value)
        {
            for (int i = 0; (i < value.Length); i = (i + 1))
            {
                this.Add(value[i]);
            }
        }

        public void AddRange(MappingCollection value)
        {
            for (int i = 0; (i < value.Count); i = (i + 1))
            {
                this.Add((IMapping)value.List[i]);
            }
        }

        public bool Contains(IMapping value)
        {
            return this.List.Contains(value);
        }

        public void CopyTo(IMapping[] array, int index)
        {
            this.List.CopyTo(array, index);
        }

        public IMapping[] ToArray()
        {
            IMapping[] array = new IMapping[this.Count];
            this.CopyTo(array, 0);

            return array;
        }

        public int IndexOf(IMapping value)
        {
            return this.List.IndexOf(value);
        }

        public void Insert(int index, IMapping value)
        {
            List.Insert(index, value);
        }

        public void Remove(IMapping value)
        {
            List.Remove(value);
        }

        public new MappingCollectionEnumerator GetEnumerator()
        {
            return new MappingCollectionEnumerator(this);
        }

        public class MappingCollectionEnumerator : IEnumerator
        {
            private IEnumerator _enumerator;
            private IEnumerable _temp;

            public MappingCollectionEnumerator(MappingCollection mappings)
            {
                _temp = ((IEnumerable)(mappings));
                _enumerator = _temp.GetEnumerator();
            }

            public IMapping Current
            {
                get { return ((IMapping)(_enumerator.Current)); }
            }

            object IEnumerator.Current
            {
                get { return _enumerator.Current; }
            }

            public bool MoveNext()
            {
                return _enumerator.MoveNext();
            }

            bool IEnumerator.MoveNext()
            {
                return _enumerator.MoveNext();
            }

            public void Reset()
            {
                _enumerator.Reset();
            }

            void IEnumerator.Reset()
            {
                _enumerator.Reset();
            }
        }
    }
}
